package com.app.jzgdirector.common.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.app.jzgdirector.R;


/**
 * 圆形ImageView，可设置最多两个宽度不同且颜色不同的圆形边框。
 *
 * @author Alan
 */
public class RoundImageView extends AppCompatImageView {
    private Bitmap mBitmap;
    private int defaultColor = 0xFFFFFFFF;
    private Drawable mDrawable;
    private Bitmap mSrcBitmap;

    private BitmapShader mShader;
    private Paint mPaint;

    private int mWidth, mHeight;

    private int mRadius;
    private int mCircleX, mCircleY;

    private int mBorderColor;
    private Paint mBorderPaint;
    private int mBorderWidth;

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
        mBorderColor = type.getColor(R.styleable.RoundProgressBar_border_outside_color,
                defaultColor);
        mBorderWidth = type.getDimensionPixelSize(R.styleable.RoundProgressBar_border_thickness, 0);
    }


    private void initView() {

        int min= Math.min(getMeasuredHeight(),getMeasuredWidth());
        mWidth=min;
        mHeight=min;
        mSrcBitmap = Bitmap.createScaledBitmap(mBitmap, mWidth, mHeight, false);
        mShader = new BitmapShader(mSrcBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setShader(mShader);

        mRadius = (min - mBorderWidth * 2) / 2;
        mCircleX = (min) / 2;
        mCircleY = (min) / 2;

        mBorderPaint = new Paint();
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStrokeJoin(Paint.Join.ROUND);
        mBorderPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        mDrawable = getDrawable();
        if (mDrawable!=null) {
            //将获得的 Drawable 转换成 Bitmap
            BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
            mBitmap = bitmapDrawable.getBitmap();
            initView();
        }
        if (mDrawable!=null) {
            canvas.save();
            float dx=(getMeasuredWidth()-mWidth)/2;
            float dy=(getMeasuredHeight()-mHeight)/2;
            canvas.translate(dx,dy);
            canvas.drawCircle(mCircleX, mCircleY, mRadius, mPaint);
            canvas.drawCircle(mCircleX, mCircleY, mRadius, mBorderPaint);
            canvas.restore();
        }
    }

}
